﻿using EquipmentManager.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EquipmentManager.BOLogic
{
    public class LoginBO : BOBase
    {
        public UserRole Login(string login, string password)
        {
            try
            {
                var user = db.Set<User>().Single(u => u.UserName == login && u.Status == EntityStatus.Active);

                CryptoBO cryptedPassword = new CryptoBO(user.Password);
                
                if (cryptedPassword.Verify(password))
                {
                    User = user;
                    return user.Role;
                }
                else
                {
                    throw new Exception("Invalid Password!");
                }
            }
            catch (Exception ex)
            {
                // Logger.Error(String.Format("Unable to login with a login ({0})", login), ex);
                throw new InvalidOperationException(String.Format("Unable to login with a login ({0})", login), ex);
            }
        }
    }

}